using System;
using ConsentManagementProviderLib.Json;
using UnityEngine;

namespace ConsentManagementProviderLib.Android
{
	internal class SpClientProxy : AndroidJavaProxy
	{
		private static readonly string NativeJavaInterfaceName = "com.sourcepoint.cmplibrary.UnitySpClient";

		internal SpConsents _spConsents;

		public SpClientProxy()
			: base(new AndroidJavaClass(NativeJavaInterfaceName))
		{
		}

		private void onUIReady(AndroidJavaObject view)
		{
			CmpDebugUtil.Log("I've reached the C# onUIReady");
			ConsentWrapperAndroid.Instance.CallShowView(view);
			ConsentMessenger.Broadcast<IOnConsentUIReady>(Array.Empty<object>());
		}

		private void onUIFinished(AndroidJavaObject view)
		{
			CmpDebugUtil.Log("I've reached the C# onUIFinished");
			ConsentWrapperAndroid.Instance.CallRemoveView(view);
			ConsentMessenger.Broadcast<IOnConsentUIFinished>(Array.Empty<object>());
		}

		private AndroidJavaObject onAction(AndroidJavaObject view, AndroidJavaObject actionType)
		{
			CmpDebugUtil.Log("I've reached the C# onAction!");
			CmpDebugUtil.Log("Trying to unwrap ActionType... ");
			CONSENT_ACTION_TYPE cONSENT_ACTION_TYPE = (CONSENT_ACTION_TYPE)actionType.Call<AndroidJavaObject>("getActionType", Array.Empty<object>()).Call<int>("getCode", Array.Empty<object>());
			CmpDebugUtil.Log("Unwrapped ActionType is: " + cONSENT_ACTION_TYPE);
			CmpDebugUtil.Log("Trying to put \"pb_key\", \"pb_value\" in pubData");
			actionType.Call<AndroidJavaObject>("getPubData", Array.Empty<object>()).Call<AndroidJavaObject>("put", new object[2] { "pb_key", "pb_value" });
			CmpDebugUtil.Log("PUT IS SUCCESSFUL");
			ConsentMessenger.Broadcast<IOnConsentAction>(new object[1] { cONSENT_ACTION_TYPE });
			CmpDebugUtil.Log("Now I'll return actionType back to Java...");
			return actionType;
		}

		private void onConsentReady(string spConsents)
		{
			CmpDebugUtil.Log("I've reached the C# onConsentReady with json string: " + spConsents);
			SpConsents spConsents2 = (_spConsents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents));
			ConsentMessenger.Broadcast<IOnConsentReady>(new object[1] { spConsents2 });
		}

		private void onSpFinished(string spConsents)
		{
			CmpDebugUtil.Log("I've reached the C# onSpFinished");
			SpConsents spConsents2 = (_spConsents = JsonUnwrapper.UnwrapSpConsentsAndroid(spConsents));
			ConsentMessenger.Broadcast<IOnConsentSpFinished>(new object[1] { spConsents2 });
		}

		private void onError(AndroidJavaObject rawThrowableObject)
		{
			CmpDebugUtil.Log("I've reached the C# onError : " + rawThrowableObject.ToString());
			Exception ex = CmpJavaToUnityUtils.ConvertThrowableToError(rawThrowableObject);
			CmpDebugUtil.Log("Exception converted successfully : " + ex.ToString());
			ConsentMessenger.Broadcast<IOnConsentError>(new object[1] { ex });
		}

		private void onConsentReady(AndroidJavaObject spConsents)
		{
		}

		private void onSpFinished(AndroidJavaObject spConsents)
		{
		}

		private void onMessageReady(AndroidJavaObject spMessage)
		{
			CmpDebugUtil.Log("I've reached the C# onMessageReady");
		}
	}
}
